package net.zhnb.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import finaler.util.Page;
import finaler.util.PageList;
import finaler.util.SQLUtil;

import net.zhnb.bean.Lottery;
import net.zhnb.dao.LotteryDao;

public class LotteryDaoImpl extends MiddleAbstractDao<Lottery> implements LotteryDao {

	@Override
	protected Class<Lottery> getEntityClass() {
		return Lottery.class;
	}

	@Override
	public int getLotteryCount(String mobile) throws Exception {
		String sql = "select count(1) from lottery where mobile='"+mobile+"'";
		return super.getIntBySql(sql);
	}

	@Override
	public int getLotteryCount(String mobile, int lun) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) from lottery a ");
		sql.append(" inner join room b on a.roomid = b.id ");
		sql.append(" inner join shower c on a.showerid = c.id ");
		sql.append(" where mobile='"+mobile+"' and lun = "+lun);
		return super.getIntBySql(sql.toString());
	}

	@Override
	public int countByMobileShowerid(String mobile, BigDecimal lun, BigDecimal showerid) throws Exception {
		String sql = "select count(1) from lottery where mobile='"+mobile+"' and lun = "+lun+" and showerid="+showerid;
		return super.getIntBySql(sql);
	}

	@Override
	public int getMaxLotteryId(String mobile) throws Exception {
		String sql = "select max(id) from lottery where mobile='"+mobile+"'";
		return super.getIntBySql(sql);
	}

	@Override
	public PageList getPageList(Page page) throws Exception {
		StringBuffer whereSql = new StringBuffer();
		whereSql.append(" where 1 = 1");
		if(page.getParameter("mobile")!=null&&!page.getParameter("mobile").equals("")){
			whereSql.append(" and a.mobile = '"+page.getParameter("mobile")+"'");
		}
		PageList pageList = new PageList(page);//固定
		StringBuffer sqlCount = new StringBuffer();
		sqlCount.append("select count(1) from lottery a");
		sqlCount.append(" inner join room b on a.roomid = b.id ");
		sqlCount.append(" inner join shower c on a.showerid = c.id ");
		sqlCount.append( whereSql );
		int dataCount = super.getIntBySql( sqlCount.toString() );//取总
		pageList.setTotal(dataCount);//总数
		
		StringBuffer queryListSql = new StringBuffer();
		queryListSql.append(" select a.*,b.roomname,c.showername from lottery a ");
		queryListSql.append(" inner join room b on a.roomid = b.id ");
		queryListSql.append(" inner join shower c on a.showerid = c.id ");
		queryListSql.append( whereSql );
		queryListSql.append(" order by a.datetime desc");
		SQLUtil pageSQL = new SQLUtil(queryListSql);//固定
		pageSQL.setFirstResult( page.getStart() );//固定
		pageSQL.setMaxResults( page.getRange() );//固定
		List<Map<String, Object>> dataList = super.findBySqlToMapList(pageSQL.getSql());//数据集
		pageList.setList(dataList);
		return pageList;
	}


}
